package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    //注入DishFlavorService对象的值
    @Autowired
    private DishFlavorService dishFlavorService;

    //涉及到多个表的增删改的操作，会有事务，需要在方法或者类前面添加事务注解
    @Override
    @Transactional
    public void saveDishAndFlavors(DishDto dishDto) {
        //1. 解决菜品名称重复，如果重复，就抛出自定义异常，某某 菜品已存在
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //select count(*) from dish where name=dishDto.getName();
        queryWrapper.eq(Dish::getName, dishDto.getName());
        int count = super.count(queryWrapper);
        if (count > 0) {//有重复的菜品名称，抛出异常
            new CustomException("菜品：" + dishDto.getName() + " 已存在");
        }
        //2. 保存菜品
//        System.out.println("保存之前的dish的id="+dishDto.getId()); //null
        super.save(dishDto);//dish的id是通过mybatis的雪花算法自动生成
//        System.out.println("保存之后的dish的id="+dishDto.getId()); //1783389390809686018
        //3.批量保存口味集合,因为要操作dish_flavor,该表对应的实体类是DishFlavor，
//         所以，我们要准备DishFlavor的mapper和service层
        //要给每一个口味设置dish的id
        List<DishFlavor> flavors = dishDto.getFlavors().stream().map((dishFlavor) -> {
            dishFlavor.setDishId(dishDto.getId());
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getDishWithFlavors(Long id) {
        //1.查询Dish的数据
        Dish dish = this.getById(id);

        //2. 查询DishFlavor对象对应的数据表的数据 flavors的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //select * from dish_flavor where dish_id =?(id)
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        //3. 组装DishDto
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);//相同属性的值copy
        dishDto.setFlavors(list); //设置dishDto的flavors属性的值
        return dishDto;
    }

    @Transactional
    @Override
    public void updateDishWithFlavors(DishDto dishDto) {
        //1. 更新菜品表
        super.updateById(dishDto);
        //2. 构建新的口味集合
        //2.1 删除之前的口味集合
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //delete from dish_flavor where dishId = ?
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //2.2 添加新的口味集合
        List<DishFlavor> flavors = dishDto.getFlavors().stream().map((dishFlavor) -> {
            dishFlavor.setDishId(dishDto.getId());
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }
}
